import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        mainlayout = QVBoxLayout()

        toolBar =QToolBar()
        toolBar.addWidget(QLabel('Current:'))
        self.currentEdit = QLineEdit()
        toolBar.addWidget(self.currentEdit)        
        mainlayout.addWidget(toolBar)
        
        self.columnView = QColumnView()
        self.columnView.clicked.connect(self.clicked)
        mainlayout.addWidget(self.columnView)
        self.data = QStandardItemModel()
        self.columnView.setModel(self.data)

        sichuan=QStandardItem('四川省')

        chengdu=QStandardItem('成都市')
        chengdu.appendRows([QStandardItem('武侯区'),QStandardItem('青羊区')])

        suining=QStandardItem('遂宁市')
        suining.appendRows([QStandardItem('船山区'),QStandardItem('安居区')])
        sichuan.appendRows([chengdu,suining])

        guangdong=QStandardItem('广东省')

        guangzhou=QStandardItem('广州市')
        guangzhou.appendRows([QStandardItem('天河区'),QStandardItem('越秀区')])
        guangdong.appendRows([guangzhou])

        self.data.appendRow(sichuan)
        self.data.appendRow(guangdong)

        self.setLayout(mainlayout) 
    def clicked(self,index):
        #currentIndex=self.columnView.currentIndex()
        currentItem=self.data.itemFromIndex(index)
        if currentItem is not None:            
            self.currentEdit.setText(currentItem.text())
            self.parent.OnShowMessage(f"current item '{currentItem.text()}'")

def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet